package com.example.homework.ui.myself;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.example.homework.R;
import com.example.homework.databinding.FragmentMyselfBinding;

/**
 *
 */
public class MyselfFragment extends Fragment {

    private Button logout;
    private TextView uname;


    private @NonNull
    FragmentMyselfBinding binding;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        MyselfViewModel myselfViewModel =
                new ViewModelProvider(this).get(MyselfViewModel.class);

        binding = FragmentMyselfBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        final TextView textView = binding.textNotifications;
        myselfViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);
        return root;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        logout = getActivity().findViewById(R.id.logout);
        uname = getActivity().findViewById(R.id.uname);
        String username = getActivity().getIntent().getStringExtra("uname");
        uname.setText(username);

        logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog();
            }
        });
    }

    /**
     * @showDialog 用来显示点击注销时的对话框
     *
     */
    private void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setTitle("温馨提示");
        builder.setMessage("退出后便签数据不会删除，但是需要重新登录才会显示，是否确定退出？点击空白处取消操作");
        builder.setPositiveButton("确定退出",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("user", Activity.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        editor.putString("username", null);
                        editor.putString("password", null);
                        editor.putString("is_remember_password", "false");
                        editor.putString("is_auto_login", "false");
                        editor.commit();
                        getActivity().finish();

                    }
                });
        AlertDialog dialog = builder.create();
        dialog.show();

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}